package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class ForbiddenWordDaoImpl extends TempDao {

	public Map<Integer,List<String>> getForbiddenWordMap(int channelId) {
		ResultSet rs = null;
		PreparedStatement call = null;
		String sql = "select * from t_s_channel_forbidden_word where channelId = " + channelId;
		Map<Integer,List<String>> forbiddenWord = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				int type =  rs.getInt("type");
				if(!forbiddenWord.containsKey(type)){
					forbiddenWord.put(type,new ArrayList<>());
				}
				forbiddenWord.get(type).add(rs.getString("content"));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return forbiddenWord;
	}

	public Set<String> getForbiddenNewWordMap() {
		ResultSet rs = null;
		PreparedStatement call = null;
		String sql = "select * from t_s_forbidden_new_word";
		Set<String> keySet = new HashSet<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				keySet.add(rs.getString("key"));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return keySet;
	}
}
